/* Copyright by GD GameDuell GmbH, All Rights Reserved
 * This document is strictly confidential and sole property of GD GameDuell GmbH, Berlin, Germany
 */

package de.gameduell.loader.src {
	import de.gameduell.framework.queue.GDAction;
	import de.gameduell.framework.queue.DelayAction;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import de.gameduell.framework.asset.AssetEvent;
	import de.gameduell.framework.asset.AssetGroup;
	import de.gameduell.framework.asset.IAsset;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.locale.LocaleData;
	import de.gameduell.framework.util.FlashVars;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.system.Security;
	import flash.text.TextField;

	/**
	 * Class LoaderBar
	 * Holds a sprite for showing the progress of the current loading task.
	 * Basically the bar animation is a movieclip with a length of 100 frames.
	 * Every frame represents a progress of 1%.
	 */
	public class LoaderBarNoobe extends MovieClip implements ILoaderBar {
		protected var asset:IAsset;
		protected var loaderClip:MovieClip;
		private var _timeline:MovieClip;
		private var _loaderBar:MovieClip;
		private var localeData:LocaleData;
		private var percentTxt:TextField;
		
		private var track25:Boolean  = true;
		private var track50:Boolean  = true;
		private var track75:Boolean  = true;
		
		private var loaderAnimTimer:Timer;
		
		/**
		 * Class constructor
		 * @param aLoaderClip	a sprite which should showing the progress
		 * @param aAssets		a list of assets to calculate the current progress
		 */
		public function LoaderBarNoobe() {
			localeData = LocaleData.getInstance();
			FlashVars.setKeyValue("purl", "http://www.gameduell.com");
			track(FlashVars.getKeyValue("language") + ".newbie.experience.tracker.loader.0");
		}
		
		public function track(tackMe:String):void {
			Debug.trace("Tracker::track: -> " + tackMe, Debug.SEND);
			try {
				if(Security.sandboxType != Security.LOCAL_WITH_FILE && ExternalInterface.available){
					ExternalInterface.call("wtCom", tackMe);
				} else {
					Debug.trace("Tracker::track: call JS function via ExternalInterface blocked because sandboxType is Security.LOCAL_WITH_FILE ", Debug.WARNING);
				}
			}
            catch (er:Error) {
				Debug.trace("Tracker::track: unable to call JS function via ExternalInterface -> error: " + er, Debug.ERROR);
			}
		}

		/**
		 * Handler for the progress event - scales the loaderBar sprite.
		 * Every time the loader made progress this function will be called.
		 */
		protected function progress(event:AssetEvent):void {			
			var percent:int = Math.round(event.asset.percentLoaded);
			percentTxt.text = percent+"%";
			var date:String = "";
			var td:Date = new Date();
			date = String("["+td.getHours()+":"+td.getMinutes()+":"+td.getSeconds()+":"+td.getMilliseconds()+"] ");
//			_loaderBar.gotoAndStop(percent);
			switch(true){
				case percent >= 25 && percent < 50 && track25:
					track25 = false;
					track(FlashVars.getKeyValue("language") + ".newbie.experience.tracker.loader.25 "+date);
					break;
				case percent >= 50 && percent < 75 && track50:
					track50 = false;
					track(FlashVars.getKeyValue("language") + ".newbie.experience.tracker.loader.50 "+date);
					break;
				case percent >= 75 && percent < 100 && track75:
					track75 = false;
					track(FlashVars.getKeyValue("language") + ".newbie.experience.tracker.loader.75 "+date);
					break;
			}
			
//			switch(true){
//				case percent >= 10 && MovieClip(_loaderBar["anim01"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim01"]).gotoAndPlay(2);
//					break;
//				case percent >= 20 && MovieClip(_loaderBar["anim02"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim02"]).gotoAndPlay(2);
//					break;
//				case percent >= 30 && MovieClip(_loaderBar["anim03"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim03"]).gotoAndPlay(2);
//					break;
//				case percent >= 40 && MovieClip(_loaderBar["anim04"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim04"]).gotoAndPlay(2);
//					break;
//				case percent >= 50 && MovieClip(_loaderBar["anim05"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim05"]).gotoAndPlay(2);
//					break;
//				case percent >= 60 && MovieClip(_loaderBar["anim06"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim06"]).gotoAndPlay(2);
//					break;
//				case percent >= 70 && MovieClip(_loaderBar["anim07"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim07"]).gotoAndPlay(2);
//					break;
//				case percent >= 80 && MovieClip(_loaderBar["anim08"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim08"]).gotoAndPlay(2);
//					break;
//				case percent >= 85 && MovieClip(_loaderBar["anim09"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim09"]).gotoAndPlay(2);
//					break;
//				case percent >= 90 && MovieClip(_loaderBar["anim10"]).currentFrame == 1:
//					MovieClip(_loaderBar["anim10"]).gotoAndPlay(2);
//					break;
//			}
			
		}
		
		public function init(assetGroup:AssetGroup, aContent:MovieClip):void {
			asset = assetGroup;
			_timeline = aContent;
			var background:Sprite = _timeline.getChildByName("background") as Sprite;
			_loaderBar = _timeline.getChildByName("loaderBar") as MovieClip;
	
			background.width = _timeline.stage.stageWidth;
			background.height = _timeline.stage.stageHeight;

			_loaderBar.gotoAndStop(1);
			percentTxt = _loaderBar.getChildByName("percentTxt") as TextField;
			
			_loaderBar.x = (_timeline.stage.stageWidth / 2) - (_loaderBar.width / 2);
			
			assetGroup.addEventListener(AssetEvent.COMPLETE, complete);
			assetGroup.addEventListener(AssetEvent.PROGRESS, progress);
			assetGroup.load();
			
			loaderAnimTimer = new Timer(6000, 10);
			loaderAnimTimer.addEventListener(TimerEvent.TIMER, onAnimTimer);
			onAnimTimer();
			loaderAnimTimer.start();
		}
		
		private function onAnimTimer(event:Event = null):void {
			_loaderBar.gotoAndPlay(2);
		}
		
		protected function complete(event:AssetEvent):void {
			loaderAnimTimer.stop();
			loaderAnimTimer.removeEventListener(TimerEvent.TIMER, onAnimTimer);
			_loaderBar.gotoAndStop(100);
			track(FlashVars.getKeyValue("language") + ".newbie.experience.tracker.loader.100");
			new DelayAction(1000, true);
			new GDAction(delayedComplete, [], true);
		}
		
		private function delayedComplete():void {
			dispatchEvent(new Event(AssetEvent.COMPLETE));
		}
	}
}